<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>查询集QuerySet | Python Web 框架要点</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C05-Database/Manager.html" />
    
    
    <link rel="prev" href="../C05-Database/CURD.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="6.5"
        data-chapter-title="查询集QuerySet"
        data-filepath="C05-Database/QuerySet.md"
        data-basepath=".."
        data-revision="Sat May 05 2018 07:17:09 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Python Web 框架要点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="AboutDjango.html">
            
                
                    <a href="../AboutDjango.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Django介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C01-CreateProject/index.html">
            
                
                    <a href="../C01-CreateProject/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        工程搭建
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C01-CreateProject/EnvInstall.html">
            
                
                    <a href="../C01-CreateProject/EnvInstall.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        环境安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C01-CreateProject/StartProject.html">
            
                
                    <a href="../C01-CreateProject/StartProject.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        创建工程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="C01-CreateProject/StartApp.html">
            
                
                    <a href="../C01-CreateProject/StartApp.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        创建子应用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="C01-CreateProject/CreateView.html">
            
                
                    <a href="../C01-CreateProject/CreateView.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        创建视图
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C02-SettingsStaticAndURL/index.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        配置、静态文件与路由
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C02-SettingsStaticAndURL/Settings.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/Settings.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        配置文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C02-SettingsStaticAndURL/StaticFiles.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/StaticFiles.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        静态文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="C02-SettingsStaticAndURL/URL.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/URL.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        路由说明
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="C02-SettingsStaticAndURL/AppConfig.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/AppConfig.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        App应用配置
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C03-RequestAndResponse/index.html">
            
                
                    <a href="../C03-RequestAndResponse/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        请求与响应
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C03-RequestAndResponse/Request.html">
            
                
                    <a href="../C03-RequestAndResponse/Request.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        请求Request
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C03-RequestAndResponse/Response.html">
            
                
                    <a href="../C03-RequestAndResponse/Response.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        响应Response
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C03-RequestAndResponse/Cookie.html">
            
                
                    <a href="../C03-RequestAndResponse/Cookie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        Cookie
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="C03-RequestAndResponse/Session.html">
            
                
                    <a href="../C03-RequestAndResponse/Session.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        Session
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C04-ClassViewAndMiddleware/index.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        类视图与中间件
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C04-ClassViewAndMiddleware/ClassView.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/ClassView.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        类视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C04-ClassViewAndMiddleware/Middleware.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/Middleware.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        中间件
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="C05-Database/index.html">
            
                
                    <a href="../C05-Database/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="C05-Database/Config.html">
            
                
                    <a href="../C05-Database/Config.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="C05-Database/Model.html">
            
                
                    <a href="../C05-Database/Model.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        定义模型类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="C05-Database/Tools.html">
            
                
                    <a href="../C05-Database/Tools.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        演示工具使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="C05-Database/CURD.html">
            
                
                    <a href="../C05-Database/CURD.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        数据库操作
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="6.5" data-path="C05-Database/QuerySet.html">
            
                
                    <a href="../C05-Database/QuerySet.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        查询集QuerySet
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="C05-Database/Manager.html">
            
                
                    <a href="../C05-Database/Manager.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        管理器Manager
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="C06-Template/index.html">
            
                
                    <a href="../C06-Template/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="C06-Template/template.html">
            
                
                    <a href="../C06-Template/template.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        模板使用
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="C07-Form/index.html">
            
                
                    <a href="../C07-Form/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        表单
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="C07-Form/form.html">
            
                
                    <a href="../C07-Form/form.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        表单使用
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="9" data-path="C08-Admin/index.html">
            
                
                    <a href="../C08-Admin/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        Admin站点
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1" data-path="C08-Admin/Config.html">
            
                
                    <a href="../C08-Admin/Config.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                        使用Admin站点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="C08-Admin/ModifyListDisplay.html">
            
                
                    <a href="../C08-Admin/ModifyListDisplay.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.2.</b>
                        
                        调整列表页展示
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.3" data-path="C08-Admin/ModifyEditDisplay.html">
            
                
                    <a href="../C08-Admin/ModifyEditDisplay.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.3.</b>
                        
                        调整编辑页展示
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.4" data-path="C08-Admin/ModifySite.html">
            
                
                    <a href="../C08-Admin/ModifySite.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.4.</b>
                        
                        调整站点信息
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.5" data-path="C08-Admin/UploadImage.html">
            
                
                    <a href="../C08-Admin/UploadImage.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.5.</b>
                        
                        上传图片
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >Python Web 框架要点</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x67E5;&#x8BE2;&#x96C6;-queryset">&#x67E5;&#x8BE2;&#x96C6; QuerySet</h1>
<h2 id="1-&#x6982;&#x5FF5;">1 &#x6982;&#x5FF5;</h2>
<p>Django&#x7684;ORM&#x4E2D;&#x5B58;&#x5728;&#x67E5;&#x8BE2;&#x96C6;&#x7684;&#x6982;&#x5FF5;&#x3002;</p>
<p>&#x67E5;&#x8BE2;&#x96C6;&#xFF0C;&#x4E5F;&#x79F0;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x96C6;&#x3001;QuerySet&#xFF0C;&#x8868;&#x793A;&#x4ECE;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x83B7;&#x53D6;&#x7684;&#x5BF9;&#x8C61;&#x96C6;&#x5408;&#x3002;</p>
<p>&#x5F53;&#x8C03;&#x7528;&#x5982;&#x4E0B;&#x8FC7;&#x6EE4;&#x5668;&#x65B9;&#x6CD5;&#x65F6;&#xFF0C;Django&#x4F1A;&#x8FD4;&#x56DE;&#x67E5;&#x8BE2;&#x96C6;&#xFF08;&#x800C;&#x4E0D;&#x662F;&#x7B80;&#x5355;&#x7684;&#x5217;&#x8868;&#xFF09;&#xFF1A;</p>
<ul>
<li>all()&#xFF1A;&#x8FD4;&#x56DE;&#x6240;&#x6709;&#x6570;&#x636E;&#x3002;</li>
<li>filter()&#xFF1A;&#x8FD4;&#x56DE;&#x6EE1;&#x8DB3;&#x6761;&#x4EF6;&#x7684;&#x6570;&#x636E;&#x3002;</li>
<li>exclude()&#xFF1A;&#x8FD4;&#x56DE;&#x6EE1;&#x8DB3;&#x6761;&#x4EF6;&#x4E4B;&#x5916;&#x7684;&#x6570;&#x636E;&#x3002;</li>
<li>order_by()&#xFF1A;&#x5BF9;&#x7ED3;&#x679C;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#x3002;</li>
</ul>
<p>&#x5BF9;&#x67E5;&#x8BE2;&#x96C6;&#x53EF;&#x4EE5;&#x518D;&#x6B21;&#x8C03;&#x7528;&#x8FC7;&#x6EE4;&#x5668;&#x8FDB;&#x884C;&#x8FC7;&#x6EE4;&#xFF0C;&#x5982;</p>
<pre><code class="lang-python">BookInfo.objects.filter(bread__gt=<span class="hljs-number">30</span>).order_by(<span class="hljs-string">&apos;bpub_date&apos;</span>)
</code></pre>
<p>&#x4E5F;&#x5C31;&#x610F;&#x5473;&#x7740;&#x67E5;&#x8BE2;&#x96C6;&#x53EF;&#x4EE5;&#x542B;&#x6709;&#x96F6;&#x4E2A;&#x3001;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x8FC7;&#x6EE4;&#x5668;&#x3002;&#x8FC7;&#x6EE4;&#x5668;&#x57FA;&#x4E8E;&#x6240;&#x7ED9;&#x7684;&#x53C2;&#x6570;&#x9650;&#x5236;&#x67E5;&#x8BE2;&#x7684;&#x7ED3;&#x679C;&#x3002;</p>
<p><strong>&#x4ECE;SQL&#x7684;&#x89D2;&#x5EA6;&#x8BB2;&#xFF0C;&#x67E5;&#x8BE2;&#x96C6;&#x4E0E;select&#x8BED;&#x53E5;&#x7B49;&#x4EF7;&#xFF0C;&#x8FC7;&#x6EE4;&#x5668;&#x50CF;where&#x3001;limit&#x3001;order by&#x5B50;&#x53E5;&#x3002;</strong></p>
<p><strong>&#x5224;&#x65AD;&#x67D0;&#x4E00;&#x4E2A;&#x67E5;&#x8BE2;&#x96C6;&#x4E2D;&#x662F;&#x5426;&#x6709;&#x6570;&#x636E;</strong>&#xFF1A;</p>
<ul>
<li>exists()&#xFF1A;&#x5224;&#x65AD;&#x67E5;&#x8BE2;&#x96C6;&#x4E2D;&#x662F;&#x5426;&#x6709;&#x6570;&#x636E;&#xFF0C;&#x5982;&#x679C;&#x6709;&#x5219;&#x8FD4;&#x56DE;True&#xFF0C;&#x6CA1;&#x6709;&#x5219;&#x8FD4;&#x56DE;False&#x3002;</li>
</ul>
<h2 id="2-&#x4E24;&#x5927;&#x7279;&#x6027;">2 &#x4E24;&#x5927;&#x7279;&#x6027;</h2>
<h4 id="1&#xFF09;&#x60F0;&#x6027;&#x6267;&#x884C;">1&#xFF09;&#x60F0;&#x6027;&#x6267;&#x884C;</h4>
<p>&#x521B;&#x5EFA;&#x67E5;&#x8BE2;&#x96C6;&#x4E0D;&#x4F1A;&#x8BBF;&#x95EE;&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x76F4;&#x5230;&#x8C03;&#x7528;&#x6570;&#x636E;&#x65F6;&#xFF0C;&#x624D;&#x4F1A;&#x8BBF;&#x95EE;&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x8C03;&#x7528;&#x6570;&#x636E;&#x7684;&#x60C5;&#x51B5;&#x5305;&#x62EC;&#x8FED;&#x4EE3;&#x3001;&#x5E8F;&#x5217;&#x5316;&#x3001;&#x4E0E;if&#x5408;&#x7528;</p>
<p>&#x4F8B;&#x5982;&#xFF0C;&#x5F53;&#x6267;&#x884C;&#x5982;&#x4E0B;&#x8BED;&#x53E5;&#x65F6;&#xFF0C;&#x5E76;&#x672A;&#x8FDB;&#x884C;&#x6570;&#x636E;&#x5E93;&#x67E5;&#x8BE2;&#xFF0C;&#x53EA;&#x662F;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x67E5;&#x8BE2;&#x96C6;qs</p>
<pre><code class="lang-python">qs = BookInfo.objects.all()
</code></pre>
<p>&#x7EE7;&#x7EED;&#x6267;&#x884C;&#x904D;&#x5386;&#x8FED;&#x4EE3;&#x64CD;&#x4F5C;&#x540E;&#xFF0C;&#x624D;&#x771F;&#x6B63;&#x7684;&#x8FDB;&#x884C;&#x4E86;&#x6570;&#x636E;&#x5E93;&#x7684;&#x67E5;&#x8BE2;</p>
<pre><code class="lang-python"><span class="hljs-keyword">for</span> book <span class="hljs-keyword">in</span> qs:
    print(book.btitle)
</code></pre>
<h4 id="2&#xFF09;&#x7F13;&#x5B58;">2&#xFF09;&#x7F13;&#x5B58;</h4>
<p>&#x4F7F;&#x7528;&#x540C;&#x4E00;&#x4E2A;&#x67E5;&#x8BE2;&#x96C6;&#xFF0C;&#x7B2C;&#x4E00;&#x6B21;&#x4F7F;&#x7528;&#x65F6;&#x4F1A;&#x53D1;&#x751F;&#x6570;&#x636E;&#x5E93;&#x7684;&#x67E5;&#x8BE2;&#xFF0C;&#x7136;&#x540E;Django&#x4F1A;&#x628A;&#x7ED3;&#x679C;&#x7F13;&#x5B58;&#x4E0B;&#x6765;&#xFF0C;&#x518D;&#x6B21;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x67E5;&#x8BE2;&#x96C6;&#x65F6;&#x4F1A;&#x4F7F;&#x7528;&#x7F13;&#x5B58;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x51CF;&#x5C11;&#x4E86;&#x6570;&#x636E;&#x5E93;&#x7684;&#x67E5;&#x8BE2;&#x6B21;&#x6570;&#x3002;</p>
<p><strong>&#x60C5;&#x51B5;&#x4E00;</strong>&#xFF1A;&#x5982;&#x4E0B;&#x662F;&#x4E24;&#x4E2A;&#x67E5;&#x8BE2;&#x96C6;&#xFF0C;&#x65E0;&#x6CD5;&#x91CD;&#x7528;&#x7F13;&#x5B58;&#xFF0C;&#x6BCF;&#x6B21;&#x67E5;&#x8BE2;&#x90FD;&#x4F1A;&#x4E0E;&#x6570;&#x636E;&#x5E93;&#x8FDB;&#x884C;&#x4E00;&#x6B21;&#x4EA4;&#x4E92;&#xFF0C;&#x589E;&#x52A0;&#x4E86;&#x6570;&#x636E;&#x5E93;&#x7684;&#x8D1F;&#x8F7D;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> booktest.models <span class="hljs-keyword">import</span> BookInfo
[book.id <span class="hljs-keyword">for</span> book <span class="hljs-keyword">in</span> BookInfo.objects.all()]
[book.id <span class="hljs-keyword">for</span> book <span class="hljs-keyword">in</span> BookInfo.objects.all()]
</code></pre>
<p><img src="../images/queryset_uncache.png" alt="&#x4E24;&#x4E2A;&#x67E5;&#x8BE2;&#x96C6;"></p>
<p><img src="../images/queyrset_uncache_result.png" alt="&#x4E24;&#x6B21;&#x67E5;&#x8BE2;"></p>
<p><strong>&#x60C5;&#x51B5;&#x4E8C;</strong>&#xFF1A;&#x7ECF;&#x8FC7;&#x5B58;&#x50A8;&#x540E;&#xFF0C;&#x53EF;&#x4EE5;&#x91CD;&#x7528;&#x67E5;&#x8BE2;&#x96C6;&#xFF0C;&#x7B2C;&#x4E8C;&#x6B21;&#x4F7F;&#x7528;&#x7F13;&#x5B58;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x3002;</p>
<pre><code class="lang-python">qs=BookInfo.objects.all()
[book.id <span class="hljs-keyword">for</span> book <span class="hljs-keyword">in</span> qs]
[book.id <span class="hljs-keyword">for</span> book <span class="hljs-keyword">in</span> qs]
</code></pre>
<p><img src="../images/queryset_cache.png" alt="&#x4E00;&#x4E2A;&#x67E5;&#x8BE2;&#x96C6;"></p>
<p><img src="../images/queryset_cache_result.png" alt="&#x4E00;&#x6B21;&#x67E5;&#x8BE2;"></p>
<h2 id="3-&#x9650;&#x5236;&#x67E5;&#x8BE2;&#x96C6;">3 &#x9650;&#x5236;&#x67E5;&#x8BE2;&#x96C6;</h2>
<p>&#x53EF;&#x4EE5;&#x5BF9;&#x67E5;&#x8BE2;&#x96C6;&#x8FDB;&#x884C;&#x53D6;&#x4E0B;&#x6807;&#x6216;&#x5207;&#x7247;&#x64CD;&#x4F5C;&#xFF0C;&#x7B49;&#x540C;&#x4E8E;sql&#x4E2D;&#x7684;limit&#x548C;offset&#x5B50;&#x53E5;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x4E0D;&#x652F;&#x6301;&#x8D1F;&#x6570;&#x7D22;&#x5F15;&#x3002;</p>
</blockquote>
<p><strong>&#x5BF9;&#x67E5;&#x8BE2;&#x96C6;&#x8FDB;&#x884C;&#x5207;&#x7247;&#x540E;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x67E5;&#x8BE2;&#x96C6;&#xFF0C;&#x4E0D;&#x4F1A;&#x7ACB;&#x5373;&#x6267;&#x884C;&#x67E5;&#x8BE2;&#x3002;</strong></p>
<p>&#x5982;&#x679C;&#x83B7;&#x53D6;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x76F4;&#x63A5;&#x4F7F;&#x7528;[0]&#xFF0C;&#x7B49;&#x540C;&#x4E8E;[0:1].get()&#xFF0C;&#x4F46;&#x662F;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x6570;&#x636E;&#xFF0C;[0]&#x5F15;&#x53D1;IndexError&#x5F02;&#x5E38;&#xFF0C;[0:1].get()&#x5982;&#x679C;&#x6CA1;&#x6709;&#x6570;&#x636E;&#x5F15;&#x53D1;DoesNotExist&#x5F02;&#x5E38;&#x3002;</p>
<p>&#x793A;&#x4F8B;&#xFF1A;&#x83B7;&#x53D6;&#x7B2C;1&#x3001;2&#x9879;&#xFF0C;&#x8FD0;&#x884C;&#x67E5;&#x770B;&#x3002;</p>
<pre><code class="lang-python">qs = BookInfo.objects.all()[<span class="hljs-number">0</span>:<span class="hljs-number">2</span>]
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C05-Database/CURD.html" class="navigation navigation-prev " aria-label="Previous page: 数据库操作"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C05-Database/Manager.html" class="navigation navigation-next " aria-label="Next page: 管理器Manager"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
